package resources.util;

import java.util.ArrayList;

import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;

public abstract class Paging {
	private ArrayList list;
	public ArrayList getList() {
		return list;
	}

	public void setList(ArrayList list) {
		this.list = list;
	}

	private ArrayList lst;
		
	private int row;
	private int currentPage;
	private ArrayList<SelectItem> selectPage;
	private int maxPage;

	public void start(ArrayList lst,int a){
		list = lst;
		row = a;
		currentPage = 1;
		maxPage = gMaxPage();
		selectPage = gSelecPage();
		updateRow();
	}
	
	private void updateRow(){
		lst = new ArrayList();
		for(int i =0;i<row;i++){
			try{
			if(null!=list.get(((currentPage-1)*row)+i))
				lst.add(list.get(((currentPage-1)*row)+i));
			}catch (IndexOutOfBoundsException e) {
				return;
			}
		}
	}
	
	private ArrayList<SelectItem> gSelecPage(){
		 ArrayList<SelectItem> lst = new ArrayList<SelectItem>();
		 for(int i=0 ;i<maxPage;i++){
			 lst.add(new SelectItem(i+1,i+1+""));
		 }
		 return lst;
	}
	
	private int gMaxPage(){
		if(list.size()%row!=0){
			return list.size()/row +1;
		}else{
			return list.size()/row;
		}
	}
	
	public void updatePage(AjaxBehaviorEvent event){
		updateRow();
	}
	
	public void nextPage(){
		if(currentPage<maxPage){
			currentPage+=1;
			updateRow();
		}
	}
	public void previousPage(){
		if(currentPage>1){
			currentPage-=1;
			updateRow();
		}
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setSelectPage(ArrayList<SelectItem> selectPage) {
		this.selectPage = selectPage;
	}

	public ArrayList<SelectItem> getSelectPage() {
		return selectPage;
	}

	public void setLst(ArrayList lst) {
		this.lst = lst;
	}

	public ArrayList getLst() {
		return lst;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public int getMaxPage() {
		return maxPage;
	}
}
